package org.leetcode.easy.leetcode700;

import org.leetcode.TreeNode;

public class Solution
{


    /**
     * v2
     */
    public TreeNode searchBST(TreeNode root, int val)
    {
        if (root==null || root.val==val)
        {
            return root;
        }

        if (root.val>val)
        {
            return searchBST(root.left,val);
        }else {
            return searchBST(root.right,val);
        }

    }


    /**
     * v1
     */
    public TreeNode searchBST2(TreeNode root, int val) {

        if (root==null)
        {
            return null;
        }

        TreeNode result = new TreeNode();

        if (root.val>val)
        {
           return result=searchBST(root.left,val);
        }else if (root.val<val)
        {
            return result=searchBST(root.right,val);
        }

        result=root;

        return result;
    }

    public static void main(String[] args) {

    // 创建节点
        TreeNode node1 = new TreeNode(1);
        TreeNode node3 = new TreeNode(3);
        TreeNode node2 = new TreeNode(2, node1, node3); // 2为根节点，1为左子节点，3为右子节点
        TreeNode node7 = new TreeNode(7); // 7没有子节点
        TreeNode root = new TreeNode(4, node2, node7); // 4为根节点，2为左子节点，7为右子节点

        Solution solution = new Solution();

        TreeNode treeNode = solution.searchBST(root, 2);
        System.out.println(treeNode.val);


    }
}
